package web

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

const (
	Success           = 0    //成功
	InternalErr       = 1000 //内部错误
	NotfoundErr       = 1001 //NOT Found
	BusinessErr       = 1002 //业务规则异常
	IlleglParamErr    = 1003 //参数错误
	RpcErr            = 1005 //远程调用错误
	DataAccessErr     = 1004 //数据访问异常，如数据持久层的各种异常
	AuthErr           = 1010 //认证失败
	PermissionErr     = 1011 //权限异常
	ResourceLockedErr = 1012 //资源占用异常
)

const (
	SuccessMsg           = "执行成功"
	InternalErrMsg       = "内部错误"
	NotfoundErrMsg       = "NOT Found"
	BusinessErrMsg       = "业务规则异常"
	IlleglParamErrMsg    = "参数错误"
	RpcErrMsg            = "远程调用错误"
	DataAccessErrMsg     = "数据访问异常，如数据持久层的各种异常"
	AuthErrMsg           = "认证失败"
	PermissionErrMsg     = "权限异常"
	ResourceLockedErrMsg = "资源占用异常"
)

func RespSuccess(context *gin.Context, data ...interface{}) {
	var resultData interface{}
	if len(data) == 1 {
		resultData = data[0]
	} else if len(data) > 1 {
		resultData = data
	}

	context.JSON(http.StatusOK, gin.H{
		"status":     Success,
		"statusInfo": SuccessMsg,
		"data":       resultData,
	})
}

func RespBizFail(context *gin.Context, message string) {
	context.JSON(http.StatusOK, gin.H{
		"status":     BusinessErr,
		"statusInfo": message,
	})
}

func RespIllFail(context *gin.Context, message string) {
	context.JSON(http.StatusOK, gin.H{
		"status":     IlleglParamErr,
		"statusInfo": message,
	})
}

func RespReqFail(context *gin.Context, message string) {
	context.JSON(http.StatusOK, gin.H{
		"status":     RpcErr,
		"statusInfo": message,
	})
}

func RespFail(context *gin.Context, code int, message string) {
	context.JSON(http.StatusOK, gin.H{
		"status":     code,
		"statusInfo": message,
	})
}

func RespFailIncludeData(context *gin.Context, code int, message string, data interface{}) {
	context.JSON(http.StatusOK, gin.H{
		"status":     code,
		"statusInfo": message,
		"data":       data,
	})
}
